//
//  popover.m
//  EFExtend
//
//  Created by mac on 15/3/10.
//  Copyright (c) 2015年 Eric. All rights reserved.
//

#import "popover.h"
#import <Masonry/Masonry.h>

@interface popover ()

@end

@implementation popover

- (void)dealloc
{
    
    HM_SUPER_DEALLOC();
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor md_amber_100];
    
    //**旧版本
    //**系统弹出框alert
    UIButten *btn = [[UIButten alloc]init];
    btn.tag = 1;
    [btn setTitle:@"系统弹出框alert" forState:UIControlStateNormal];
    btn.titleLabel.backgroundColor = [[UIColor flatBelizeHoleColor] colorWithAlphaComponent:.5];
    
    [btn setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
//    btn.imageSize = CGSizeMake(30, 40);
//    btn.buttenType = UIButtenTypeIconSideLeft;
    
    btn.backgroundColor = [UIColor flatAmethystColor];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(15);
        make.centerX.mas_equalTo(self.view);
    }];
    [btn onClicked:^(NSString *state) {
        INFO(state);
        HMUIAlertView *aler = nil;
        
        Class alertZ = [HMUIAlert7View class];
        if (IOS9_OR_LATER) {
             alertZ = [HMUIAlertView class];
        }
        aler = [alertZ alertWithTitle:@"弹出框" message:@"提示信息"];
        [aler addButtonWithTitle:@"确定"];
        [aler addCancelTitle:@"取消"];
        
        [aler timeoutSec:5 toClick:0 animated:YES];
        [aler onClicked:^(HMUIAlertView *alert, NSInteger index) {
            INFO(@(index));
        }];
        [aler show];
        
        HMUIActionSheet *sheet = nil;
        
        Class sheetZ = [HMUIAction7Sheet class];
        if (IOS9_OR_LATER) {
            sheetZ = [HMUIActionSheet class];
        }
        sheet = [sheetZ alertWithTitle:@"弹出框" message:nil];//[[HMUIActionSheet alloc]initWithTitle:@"弹出框" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"关闭" otherButtonTitles:@"确定", nil];
        [sheet addButtonWithTitle:@"确定"];
        [sheet addCancelTitle:@"取消"];
        [sheet timeoutSec:5 toClick:0 animated:YES];
        [sheet onClicked:^(HMUIActionSheet *sheet, NSInteger index) {
            
        }];
        [sheet show];
    }];
    
    //**系统弹出sheet
    btn = [[UIButten alloc]init];
    btn.tag = 2;
    [btn setTitle:@"系统弹出框sheet" forState:UIControlStateNormal];
    btn.titleLabel.backgroundColor = [[UIColor flatBelizeHoleColor] colorWithAlphaComponent:.5];
    
    [btn setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
//    btn.buttenType = UIButtenTypeCheckBoxSideRight|UIButtenTypeCheckBoxClickedInside;
//    btn.imageSize = CGSizeMake(30, 40);
    
    btn.backgroundColor = [UIColor flatAmethystColor];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([self.view viewWithTag:1].mas_bottom).offset(10);
        make.centerX.mas_equalTo(self.view);
    }];
    [btn onClicked:^(NSString *state) {
        INFO(state);
        HMUIActionSheet *sheet = nil;
        
        Class sheetZ = [HMUIAction7Sheet class];
        if (IOS8_OR_LATER) {
            sheetZ = [HMUIActionSheet class];
        }
        sheet = [sheetZ alertWithTitle:@"弹出框" message:nil];
        [sheet addButtonWithTitle:@"确定"];
        [sheet addCancelTitle:@"取消"];
        [sheet timeoutSec:5 toClick:0 animated:YES];
        [sheet onClicked:^(HMUIActionSheet *sheet, NSInteger index) {
            
        }];
        [sheet show];
    }];
    //**新版本
    if (IOS8_OR_LATER) {
        btn = [[UIButten alloc]init];
        btn.tag = 3;
        [btn setTitle:@"新版弹出框alert" forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor flatEmeraldColor];
        [self.view addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo([self.view viewWithTag:2].mas_bottom).offset(10);
            make.centerX.mas_equalTo(self.view);
        }];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"弹出框alert" message:@"提示信息" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            
        }];
        [alert addAction:action];
        
        action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }];
        [alert addAction:action];
        
        action = [UIAlertAction actionWithTitle:@"关闭" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            
        }];
        [alert addAction:action];
        
        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.placeholder = @"请输入什么都会";
        }];
        
        [btn onClicked:^(NSString *state) {
            [self presentViewController:alert animated:YES completion:^{
                
            }];
        }];
        [[HMURLMap boardForMap:@"*"] presentViewController:alert animated:NO completion:^{
            
        }];
        
        btn = [[UIButten alloc]init];
        btn.tag = 4;
        [btn setTitle:@"新版弹出框sheet" forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor flatEmeraldColor];
        [self.view addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo([self.view viewWithTag:3].mas_bottom).offset(10);
            make.centerX.mas_equalTo(self.view);
        }];
        UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"弹出框sheet" message:@"提示信息" preferredStyle:UIAlertControllerStyleActionSheet];
        action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            
        }];
        [sheet addAction:action];
        
        action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }];
        [sheet addAction:action];
        
        [btn onClicked:^(NSString *state) {
            [self presentViewController:sheet animated:YES completion:^{
                
            }];
        }];
        if (!self.presentedViewController) {
            [self presentViewController:sheet animated:YES completion:^{
                
            }];
        }
        
    }
    

    //提示框
    btn = [[UIButten alloc]init];
    btn.tag = 5;
    btn.buttenType = UIButtenTypeIconSideTop;
    [btn EFImage:@"back.png"];
//    [btn setTitle:@"提示框" forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor flatEmeraldColor];
    
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([self.view viewWithTag:IOS8_OR_LATER?4:2].mas_bottom).offset(10);
        make.centerX.mas_equalTo(self.view);
    }];
    [btn onClicked:^(NSString *state) {
        //目前没有开放提示框的个性化设置，日后有需求再开放吧
//        [self showMessageTip:@"提示框" detail:@"提示信息" timeOut:3.f];
        
        [self delayInMain:1.f invoke:^{
//            [self tipsDismiss];
            [btn EFText:@"1234"];

            [self delayInMain:1.f invoke:^{
                [btn EFText:@"12345"];

                [self showLoaddingTip:@"提示信息" timeOut:4.f];
                [self delayInMain:4.f invoke:^{
                    [btn EFText:@"0"];
                
//                    [self tipsDismiss];
//                    [self backWithAnimate:YES remove:YES];
                }];
            }];
        }];
    }];
    
      //**自定义弹出框popover
    btn = [[UIButten alloc]init];
    btn.tag = 6;
    btn.tagString = @"upPopover";
    [btn setTitle:@"上" forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor flatEmeraldColor];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([self.view viewWithTag:5].mas_bottom).offset(10);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(40);
    }];
    
    
    [self.customNavLeftBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [self.customNavLeftBtn setFrame:CGRectMakeBound(32, 32)];
    
    [self.customNavRightBtn setImagePrefixName:nil title:@"弹出框"];
    [self.customNavRightBtn setFrame:CGRectMakeBound(100, 32)];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

ON_Button(signal){
    UIButten *btn = signal.source;
    if ([signal is:[UIButten TOUCH_UP_INSIDE]]) {
        if ([btn is:@"leftBtn"]) {
            [self backAndRemoveWithAnimate:YES];
        }else if ([btn is:@"rightBtn"]){
            HMUIDatePicker *pi = [HMUIDatePicker spawnWithReferView:btn inView:nil];
            pi.datePickerStyle = UIDatePickerStyle_Calendar;
            pi.backgroundStyle = UIPopoverBackgroundStyle_defatul;
            pi.date = [NSDate date];
            pi.calendarPicker.dayMinHorSpace = 5;
            [pi showWithAnimated:YES];
        }else if ([btn is:@"upPopover"]){
            HMUIPopoverView *pi = [HMUIPopoverView spawnWithReferView:btn inView:nil];
            pi.backgroundStyle = UIPopoverBackgroundStyle_glass;
            pi.referHighlightRadius = 5;
            UILabel *txt = [[UILabel alloc]init];
            txt.text = @"提示信息";
            txt.backgroundColor = RGB(1, 1, 0);
            txt.size = CGSizeMake(100, 50);

            pi.contentView = txt;
            [pi showWithAnimated:YES];
        }else if ([btn is:@"popover"]){
            HMUIDatePicker *pi = [HMUIDatePicker spawnWithReferView:btn inView:nil];
            //还有三种弹出方式，自己看效果吧
            //        HMUIDatePicker *pi = [HMUIDatePicker spawnWithReferView:nil inView:self.view];
            //        HMUIDatePicker *pi = [HMUIDatePicker spawnWithReferView:btn inView:self.view];
            //        HMUIDatePicker *pi = [HMUIDatePicker spawnWithReferView:nil inView:nil];
            //样式
            //        pi.containerMode = UIPopoverContainerModeBottom;
//            pi.popoverArrowDirection = UIPopoverArrowDirectionRight;
            //不显示模糊背景
            pi.backgroundStyle = UIPopoverBackgroundStyle_shadow;
            
            /*不带容器*/
            //        pi.contentCustom = YES;
            //设置主题颜色
            pi.solidFillColor = [[UIColor yellowColor] colorWithAlphaComponent:.5f];
            //边框阴影颜色
            pi.shadowColor = [UIColor blackColor];
            //边框颜色
            pi.borderColor = [[UIColor grayColor] colorWithAlphaComponent:.5f];
            
            pi.referHighlightRadius = 15;
            //针对 DatePick的设置
            //自己看效果
            pi.datePickerStyle = UIDatePickerStyle_Calendar;
            //        pi.datePickerStyle = UIDatePickerStyle_Date;
            
            //HMUIPopoverView可以自定义视图内容
            //#define MODE1
            UILabel *txt = [[UILabel alloc]init];
            txt.text = @"提示信息";
            txt.backgroundColor = RGB(1, 1, 0);
            
#ifdef MODE1
            [txt updateConstraints];
            UIView *v = [UIView spawn];
            [v addSubview:txt];
            v.backgroundColor = RGB(1, 0, 0);
#endif
            
            [txt mas_makeConstraints:^(MASConstraintMaker *make) {
#ifdef MODE1
                make.top.mas_equalTo(0);
                make.left.mas_equalTo(0);//如果txt没有父视图，加这两条会报错，因为属性找不到关联
#endif
                
            }];
            
#ifdef MODE1
            v.size = [txt systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
            [pi setContentView:v autolayout:^(UIView *contentView) {
            }];
#endif
            
            
            /*关于autolayout多说几句
             *   在一个控件内存在autolayout与autoresize子控件时，要适配部分autoresize
             *   translatesAutoresizingMaskIntoConstraints 默认是打开转换的
             //在autoresize下可以通过以下代码进行autolayout视图的大小计算
             if (!contentView.translatesAutoresizingMaskIntoConstraints) {
             [contentView needsUpdateConstraints];
             [contentView updateConstraints];
             CGSize ss = [contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
             
             }
             //另外如果autolayout视图在autoresize视图内部，而且需要配置相对位置，可以通过以下代码设置
             [autoresizeView removeConstraints:autoresizeView.constraints];//或者只删除配置内容相关的
             
             NSDictionary *dic = NSDictionaryOfVariableBindings(autolayout);
             NSString *VFL = [NSString stringWithFormat:@"V:|-%f-[autolayout]",rect.origin.y];
             NSArray *ar = [NSLayoutConstraint constraintsWithVisualFormat:VFL options:0 metrics:nil views:dic];
             [autoresizeView addConstraints:ar];
             
             VFL = [NSString stringWithFormat:@"|-%f-[autolayout]",rect.origin.x];
             ar = [NSLayoutConstraint constraintsWithVisualFormat:VFL options:0 metrics:nil views:dic];
             
             [autoresizeView addConstraints:ar];
             */
            
            [pi showWithAnimated:YES];
            //请自行监听 ON_DatePicker
        }
    }
}

ON_WebAPI(dlg){
    if (dlg.sending) {
        
        
    }else if (dlg.succeed){
        NSDictionary *dic = [dlg.output objectForKey:[HMWebAPI params]];
        
        
    }else if (dlg.failed){
        if (dlg.timeout) {
            [self showFailureTip:@"操作失败" detail:@"链接超时" timeOut:3.f];
            return;
        }
        
        [self showFailureTip:@"网络链接失败" detail:@"数据不存在或网络错误" timeOut:3.f];
    }else if (dlg.cancelled){
        
    }
    //#if (__ON__ == __HM_DEVELOPMENT__)
    //    CC( [self.class description],dlg.responseData?dlg.responseData:dlg.errorDesc);
    //#endif	// #if (__ON__ == __BEE_DEVELOPMENT__)
}
@end
